var amapFile = require('amap-wx');
function getLocation(key,markers=[]) {
  let self=this
  var myAmapFun = new amapFile.AMapWX({key: key});
  myAmapFun.getRegeo({
    success: function(res){
      //成功回调
      let data=res[0];
      if(markers.length>0){
        markers[0].latitude=data.latitude;
        markers[0].longitude=data.longitude;
      }
      var regeocodeData=data.regeocodeData;
      var addressComponent=regeocodeData.addressComponent;
      let address=regeocodeData.formatted_address;

      let province=addressComponent.province,
      city=addressComponent.city,
      district=addressComponent.district,
      street=addressComponent.township;
      let result={
        latitude:data.latitude,
        longitude:data.longitude,
        start_address:address,
        markers:markers,
        city:city,
        province:province,
        street:street,
        district:district,
        name:data.name
      }
      wx.setStorageSync("mapinfo",result);
    },
    fail: function(error){
      //失败回调

    }
  })

};

function getByCity(key,city) {
  let self=this
  var myAmapFun = new amapFile.AMapWX({key: key});
  myAmapFun.getRegeo({
    city:city,
    success: function(res){
      //成功回调
    },
    fail: function(error){
      //失败回调
    }
  })

};

module.exports = {
  getLocation: getLocation,
};
